package com.systop.asset.controller;

import java.util.List;


import com.systop.asset.entity.AssetCategories;
import com.systop.asset.entity.RestResponse;
import com.systop.asset.entity.vo.AssetCategoryVo;
import com.systop.asset.entity.vo.LocationVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.systop.asset.entity.Location;
import com.systop.asset.service.ILocationService;

/**
 * 资产位置Controller
 * 
 * @author mc
 * @date 2025-06-25
 */
@RestController
@RequestMapping("/system/location")
public class LocationController
{
    @Autowired
    private ILocationService locationService;

    /**
     * 查询资产位置列表
     */
    @GetMapping("/list")
    public RestResponse list(Location location)
    {

        List<Location> list = locationService.getLocation(location);
        return RestResponse.success(list);
    }


    /**
     * 获取资产位置详细信息
     */
    @GetMapping(value = "/{id}")
    public RestResponse getInfo(@PathVariable("id") Long id)
    {
        return RestResponse.success(locationService.selectLocationById(id));
    }

    /**
     * 新增资产位置
     */

    @PostMapping
    public RestResponse add(@RequestBody Location location)
    {
        if(locationService.insertLocation(location)!=0){
            return RestResponse.success();
        }
        return RestResponse.failure(500,"添加失败,名称已存在");
    }

    /**
     * 修改资产位置
     */

    @PutMapping
    public RestResponse edit(@RequestBody Location location)
    {
        if(locationService.updateLocation(location)!=0){
            return RestResponse.success();
        }
        return RestResponse.failure(500,"修改失败,名称已存在");
    }

    /**
     * 删除资产位置
     */

	@DeleteMapping("/{ids}")
    public RestResponse remove(@PathVariable Long[] ids)
    {
        //删除时判断是否包含子分类
        if(locationService.selectLocationById(ids[0]).getParentId() == 0L){
            return RestResponse.failure(500,"包含子位置，请先删除子位置");
        }
        return RestResponse.success(locationService.deleteLocationByIds(ids));
    }
    /**
     * 查询资产分类列表
     */
    @GetMapping("/allList")
    public RestResponse allList(Location location)
    {
        List<LocationVo> list = locationService.allList(location);
        return RestResponse.success(list);
    }

}
